﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using WebDotNet.Extensions;
using System.Net;

namespace WebDotNet.Samples.WdnMicroBlog.App.Handlers
{
	public class HomePage
	{
		public StringBuilder Get(HttpContextBase httpContextBase)
		{
			List<Entites.Entry> entries = new XmlRepository<Entites.Entry>()
				.List()
				.OrderByDescending(e => e.CreatedOn)
				.ToList();

			return httpContextBase.RenderPage("homepage", entries);
		}

		public StringBuilder Post(HttpContextBase httpContextBase)
		{
			Entites.Entry entry = new Entites.Entry();
			entry.Content = httpContextBase.Request.Form["content"];
			entry.CreatedOn = DateTime.Now;
			entry.Id = Guid.NewGuid();
			entry.Slug = httpContextBase.Request.Form["slug"];
			entry.Title = httpContextBase.Request.Form["title"];
			entry.Comments = new List<Entites.Comment>();

			new XmlRepository<Entites.Entry>()
				.Add(entry)
				.Save();

			httpContextBase.SeeOther("/");

			return null;
		}
	}

	public class Entry
	{
		public StringBuilder Get(HttpContextBase httpContextBase, string slug)
		{
			Entites.Entry entry = new XmlRepository<Entites.Entry>()
				.List()
				.FirstOrDefault(e => e.Slug == slug);

			if (entry == null)
			{
				httpContextBase.NotFound();
				return null;
			}

			return httpContextBase.RenderPage("entry", entry);
		}

		public StringBuilder Post(HttpContextBase httpContextBase, string slug)
		{
			XmlRepository<Entites.Entry> xmlRepository = new XmlRepository<Entites.Entry>();

			Entites.Entry entry = xmlRepository
				.List()
				.FirstOrDefault(e => e.Slug == slug);

			if (entry == null)
			{
				httpContextBase.NotFound();
				return null;
			}

			Entites.Comment comment = new Entites.Comment();
			comment.Content = httpContextBase.Request.Form["content"];
			comment.CreatedOn = DateTime.Now;
			comment.Email = httpContextBase.Request.Form["email"];
			comment.EntryId = entry.Id;
			comment.Id = Guid.NewGuid();
			comment.Name = httpContextBase.Request.Form["name"];
			comment.Url = httpContextBase.Request.Form["url"];


			if (entry.Comments == null)
			{
				entry.Comments = new List<Entites.Comment>();
			}

			entry.Comments.Add(comment);

			xmlRepository
				.Update(entry)
				.Save();

			httpContextBase.SeeOther(string.Format("/{0}", entry.Slug));
			return null;
		}
	}

	public class Feed
	{
		public StringBuilder Get(HttpContextBase httpContextBase)
		{
			List<Entites.Entry> entries = new XmlRepository<Entites.Entry>()
				.List()
				.OrderByDescending(e => e.CreatedOn)
				.ToList();

			httpContextBase.SetResponseHeader(HttpResponseHeader.ContentType.ToString(), "application/rss+xml", true);

			if (entries.Any())
			{
				httpContextBase.SetResponseHeader(HttpResponseHeader.LastModified.ToString(), entries[0].CreatedOn.ToUniversalTime().ToString("r"), true);
			}

			return httpContextBase.RenderPage("feed", entries);
		}
	}
}
